<?php
/**
 * @新闻类
 * 1、新闻列表
 * 2、新闻详情
 * 3、新闻编辑/添加
 * 4、新闻删除
 * 5、新闻开启禁用
 * 
 * @作者 
 * 万全强（php）
 */
namespace app\controller;

use app\controller\HomeBase;
use think\facade\Db;


use app\validate\NewsValidate;
use app\model\NewsModel;


class News extends HomeBase
{

    private $NewsValidate;
    private $NewsModel;
    private $RoleId=0;

    public function __construct(){
               
        parent::__construct();
      
         $this->NewsValidate=new NewsValidate();
         $this->NewsModel=new NewsModel();
        
    }
    /**
     * 新闻列表
     *
     * @return void
     */
    public function GetNewsList()
    {
        $param=request()->param();
        $where="1=1 and status !=-1";
        if(isset($param['keywords'])&&!empty($param['keywords'])){
            $where.=" and title like '%".trim($param['keywords'])."%'";
        }
        if(isset($param['status'])&&strlen($param['status'])>0){
            $where.=" and status = '{$param['status']}'";
        }
        if(isset($param['id'])&&!empty($param['id'])){
        	$where.=" and role_id=".$param['id'];
        }else{
        	if($this->user_id!=1){
        		$where.=" and role_id=".$this->role_id;
        	}
        }
       
        $res=$this->NewsModel->GetList($where);
        return json(['status'=>1,'msg'=>'获取成功','data'=>$res]);
    }
    /**
     * 新闻详情
     *
     * @return void
     */
    public function GetNewsDetail()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0; 
        $result=$this->NewsModel->detail($id);
        
        return json(['status'=>1,'msg'=>'获取成功','data'=>$result]);
       
    }
    /**
     * 新闻添加/编辑
     *
     * @return void
     */
    public function NewsEdit()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0;
    
        //表单验证
        try {
           $re=$this->validate($param,'app\validate\NewsValidate');
        } catch (\Exception $e) {       
            return json(['status'=>0,'msg'=>$e->error,'data'=>[]]);
        }
        
        //管理表
        if($id){
            $id= $this->NewsModel->EditData($param,$id);
        }else{
        	$param['role_id']=$this->role_id;
            $id= $this->NewsModel->AddData($param);
        }
       
        return json(['status'=>1,'msg'=>'操作成功','data'=>['id'=>$id]]);
    }
    /**
     * 新闻删除
     *
     * @return void
     */
    public function NewsDel()
    {
        $param=request()->param();
        $id=isset($param['id'])?trim($param['id']):'';
        
        $result=$this->NewsModel->Del(explode(',',$id));
        if($result){
            return json(['status'=>1,'msg'=>'删除成功','data'=>[]]);
        }else{
            return json(['status'=>0,'msg'=>'删除失败','data'=>[]]);
        }
    }
    /**
     * 新闻是否开启禁用
     *
     * @return void
     */
    public function UpdateNewsStatus()
    {
        $param=request()->param();
        $id=isset($param['id'])?intval($param['id']):0;
        
        $result=$this->NewsModel->UpdateStatus($id);
        if($result){
            return json(['status'=>1,'msg'=>'操作成功','data'=>[]]);
        }else{
            return json(['status'=>0,'msg'=>'操作失败','data'=>[]]);
        }
    }
    
}
